package com.ningxun.pagent.web.controller.wechatpolicy;

import com.ningxun.pagent.bus.domain.BusPolicyContent;
import com.ningxun.pagent.bus.domain.vo.WechatLatestPolicyVo;
import com.ningxun.pagent.bus.domain.vo.WechatPolicyContentDetailVo;
import com.ningxun.pagent.bus.service.IBusPolicyContentService;
import com.ningxun.pagent.common.core.controller.BaseController;
import com.ningxun.pagent.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/wechat/policy")
public class WechatPolicyController extends BaseController {

    @Autowired
    private IBusPolicyContentService busPolicyContentService;

    //根据id查详情
    @GetMapping("/{policyId}")
    public AjaxResult getInfoByRequireId(@PathVariable("policyId") Long policyId) {
        BusPolicyContent item = busPolicyContentService.selectBusPolicyContentByPolicyId(policyId);
        WechatPolicyContentDetailVo vo = WechatPolicyContentDetailVo.convertToWechatPolicyContentVo(item);
        return AjaxResult.success(Collections.singletonList(vo));
    }

    //获取最新政策
    @GetMapping("/latest")
    @ResponseBody
    public AjaxResult latestTwo() {
        List<BusPolicyContent> list = busPolicyContentService.selectLatestPolicies(2);
        // 使用stream转换列表
        List<WechatLatestPolicyVo> voList = list.stream()
                .map(WechatLatestPolicyVo::convertToWechatLatestPolicyVo)
                .collect(Collectors.toList());
        return success(voList);
    }
}
